package com.cwd.arm.application.service.read;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwd.arm.common.dto.session.AuthDto;
import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.common.enums.AuthTypeEnum;
import com.cwd.arm.infrastructure.dao.AuthDao;
import com.cwd.arm.infrastructure.dao.RoleDao;
import com.cwd.arm.infrastructure.po.AuthPo;
import com.cwd.arm.infrastructure.po.RolePo;
import com.cwd.arm.application.dto.RoleDto;
import com.cwd.arm.application.service.read.facade.RoleReadService;
import com.cwd.arm.infrastructure.po.UserPo;
import com.cwd.arm.infrastructure.query.AuthDaoQuery;
import com.cwd.arm.infrastructure.query.RoleDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:角色
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Repository
@Primary
public class RoleReadServiceImpl implements RoleReadService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private AuthDao authDao;

    @Override
    public RoleDto selectById(Long id) {
        return convertToDto(roleDao.getById(id));
    }


    @Override
    public Page<RoleDto> selectPage(RoleDaoQuery daoQuery) {
        Page<RolePo> poPage = roleDao.selectPage(daoQuery);
        Page<RoleDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(poPage, dtoPage);
        List<RoleDto> dtoList = poPage.getRecords().stream()
                .map(po -> convertToDto(po)).collect(Collectors.toList());
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }

    @Override
    public List<RoleDto> selectList() {
        List<RolePo> poList = roleDao.selectList(new RoleDaoQuery());
        return convertToDtoList(poList);
    }

    private List<RoleDto> convertToDtoList(List<RolePo> poList) {
        List<RoleDto> roleDtoList = new ArrayList<>(10);
        for (RolePo rolePo : poList) {
            RoleDto dto = new RoleDto();
            BeanUtils.copyProperties(rolePo, dto);
            roleDtoList.add(dto);
        }
        return roleDtoList;
    }

    private RoleDto convertToDto(RolePo po) {
        RoleDto dto = new RoleDto();
        BeanUtils.copyProperties(po, dto);
        AuthDaoQuery daoQuery = new AuthDaoQuery();
        daoQuery.setRoleId(po.getId());
        daoQuery.setAuthType(AuthTypeEnum.BTN.getCode());
        List<AuthPo> btnAuthList = authDao.selectByQuery(daoQuery);
        dto.setBtnAuthList(convertToAutoDto(btnAuthList));
        daoQuery.setAuthType(AuthTypeEnum.MENU.getCode());
        List<AuthPo> menuAuthList = authDao.selectByQuery(daoQuery);
        dto.setMenuAuthList(convertToAutoDto(menuAuthList));
        return dto;
    }

    private List<AuthDto> convertToAutoDto(List<AuthPo> authPoList) {
        return authPoList.stream().map(po -> (convertToAutoDto(po))).collect(Collectors.toList());
    }

    private AuthDto convertToAutoDto(AuthPo authPo) {
        AuthDto authDto = new AuthDto();
        BeanUtils.copyProperties(authPo, authDto);
        return authDto;
    }

}
